import { createPinia, defineStore } from "pinia";
import piniaPluginPersistedstate from "pinia-plugin-persistedstate";
import piniaPersistConfig from "@/config/piniaPersist";

export interface GlobalState {
	token: string;
	language: string;
}

export const useGlobalStore = defineStore("global", {
	state: (): GlobalState => {
		return {
			// token
			token: "",
			// language
			language: ""
		};
	},
	actions: {
		// setToken
		setToken(token: string) {
			this.token = token;
		},
		// updateLanguage
		updateLanguage(language: string) {
			this.language = language;
		}
	},
	// 持久化
	persist: piniaPersistConfig("GlobalState")
});

const pinia = createPinia();
// 持久化
pinia.use(piniaPluginPersistedstate);

export default pinia;
